package com.fs.controller.v1;

import com.fs.controller.vo.cleaner.ReqAddCleaner;
import com.fs.controller.vo.cleaner.ReqAddCleanerByRoom;
import com.fs.controller.vo.cleaner.ReqGetCleaner;
import com.fs.controller.vo.cleaner.RespCleaner;
import com.fs.global.init.RoleName;
import com.fs.service.CleanerService;
import com.fs.service.RoomService;
import com.fs.service.bo.cleaner.BoCleaner;
import com.fs.service.bo.cleaner.BoCreateParam;
import com.fs.service.bo.cleaner.BoQueryParam;
import com.fs.service.bo.room.BoCleanerCreateParam;
import com.fs.service.bo.room.BoRoom;
import com.fs.service.bo.shop.BoShop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags = "保洁人员管理",description = "管理员账户使用")
@RestController
@RequestMapping("/v1/cleaner")
@RequiresRoles(RoleName.ADMIN)
@Validated
public class CleanerController {
    private final CleanerService cleanerService;
    private final RoomService roomService;

    public CleanerController(CleanerService cleanerService, RoomService roomService) {
        this.cleanerService = cleanerService;
        this.roomService = roomService;
    }

    @ApiOperation("添加房间保洁")
    @PostMapping("/by/shop")
    public RespCleaner addCleanerByShop(@RequestBody @Valid ReqAddCleaner req) {
        BoCreateParam param = new BoCreateParam();
        param.setShopId(req.getShopId())
                .setName(req.getName())
                .setPhone(req.getPhone());

        BoCleaner bo = cleanerService.addCleaner(param);
        return new RespCleaner(bo);
    }

    @ApiOperation("添加房间保洁")
    @PostMapping
    public RespCleaner addCleanerByRoom(@RequestBody @Valid ReqAddCleanerByRoom req) {
        BoRoom room = roomService.getByID(req.getRoomId());

        BoCreateParam param = new BoCreateParam();
        param.setShopId(room.getShop().getId())
                .setName(req.getName())
                .setPhone(req.getPhone());

        BoCleaner bo = cleanerService.addCleaner(param);
        return new RespCleaner(bo);
    }

    @GetMapping("/cleaners")
    @ApiOperation("查询保洁人员")
    public List<RespCleaner> getCleaners(ReqGetCleaner req) {
        BoQueryParam param = new BoQueryParam();
        param.setName(req.getName())
                .setShopId(req.getShopId())
                .setName(req.getName());

        return cleanerService.getCleaners(param).stream().map(RespCleaner::new).collect(Collectors.toList());
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除保洁人员")
    public void deleteCleaner(@PathVariable Integer id) {
        cleanerService.deleteCleanerById(id);
    }

}
